package com.jshoperxms.controller.ldnsbs;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.YdTeamT;
import com.jshoperxms.service.YdTeamTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by sdywcd on 2019/2/27.
 * Des:
 */
@RestController
@RequestMapping("/ldnsbs/ldteam")
public class LdTeamContoller {

    @Resource
    private YdTeamTService ydTeamTService;


    /**
     * 查询小队列表
     * @param searchModel
     * @return
     */
    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    public ResponseEntity findByPage(@RequestBody SearchModel searchModel){
        Map<String, Object> responsedata = new HashMap<>();
        findCustomSearch(searchModel,responsedata);
        return Response.success(responsedata);
    }


    private void findCustomSearch(SearchModel searchModel, Map<String, Object> responsedata) {
        if(StringUtils.isNotBlank(searchModel.getFilter())){
            filterSearch(searchModel,responsedata);
        }else{
            findDefaultAll(searchModel,responsedata);
        }
    }

    private void filterSearch(SearchModel searchModel, Map<String, Object> responsedata){
        int currentPage=searchModel.getStart();
        int lineSize=searchModel.getLength();
        Criterion criterion= Restrictions.and(Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState())).add(Restrictions.like("teamname",searchModel.getFilter(), MatchMode.ANYWHERE));
        int records=this.ydTeamTService.count(YdTeamT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<YdTeamT> list=this.ydTeamTService.findByCriteriaByPage(YdTeamT.class,criterion, Order.desc(TagWords.UPDATETIME),currentPage,lineSize);
        this.processPageList(list,responsedata);
    }


    private void findDefaultAll(SearchModel searchModel,Map<String,Object> responsedata){
        int currentPage=searchModel.getStart();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        int records=this.ydTeamTService.count(YdTeamT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order=Order.asc(TagWords.UPDATETIME);
        if(StringUtils.isNotBlank(searchModel.getSortBy())){
            if(searchModel.isSortDesc()){
                order=Order.desc(searchModel.getSortBy());
            }else{
                order=Order.asc(searchModel.getSortBy());
            }
        }
        List<YdTeamT> list=this.ydTeamTService.findByCriteriaByPage(YdTeamT.class,criterion, order,currentPage,lineSize);
        this.processPageList(list,responsedata);

    }

    private void processPageList(List<YdTeamT> list,Map<String,Object> responsedata) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for(Iterator<YdTeamT>it=list.iterator();it.hasNext();){
            YdTeamT bean=it.next();
            if(StringUtils.equals("1",bean.getType())){
                bean.setType("公开");
            }else{
                bean.setType("隐私");
            }
            Map<String, Object> cellMap = new HashMap<String, Object>();
            cellMap.put("id", bean.getId());
            cellMap.put("teamname",bean.getTeamname());
            cellMap.put("leadername",bean.getLeadername());
            cellMap.put("teamdes",bean.getTeamdes());
            cellMap.put("teamavatarurl",bean.getTeamavatarurl());
            cellMap.put("currentmember",bean.getCurrentmember()+"/"+bean.getMaxmember());
            cellMap.put("teamcategory",bean.getTeamcategory());
            cellMap.put("teamgrowstatus",bean.getTotalleafs()+"/"+bean.getHotper()+"/"+bean.getTotalyuedu()+"/"+bean.getTotalshare());
            cellMap.put("teamtype",bean.getType());
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime()));
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA,data);

    }

}
